import * as axios from 'axios'

import {Message, Notification} from 'element-ui'

let service = axios.create({
  baseURL: 'https://testcefa.qimiaoxue.com',//域名信息-测试
  timeout: 10000
})

// 请求拦截 可在请求头中加入token等
service.interceptors.request.use(config => {
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截 对响应消息作初步的处理
service.interceptors.response.use(resp => {
  if (resp.data) {
    if (resp.data.code !== 1) {
      Message({
        type: 'error',
        message: resp.data.msg,
        duration: 5000
      })
    }
    return {code: resp.data.code, data: resp.data.data || {}, msg: resp.data.msg}
  } else {
    return resp
  }
}, error => {
  if (error.response) {
    switch (error.response.states) {
      case 400: {
        if (error.response && error.response.data && error.response.data.msg) {
          Notification.error({
            title: '400错误',
            message: error.response.data.msg,
            duration: 5000,
            closable: true
          })
        }
        break
      }
    }
  }
  return Promise.reject(res)
})

export default service
